<!DOCTYPE html>
<html>
<head>
<title>oops!homework7--testQRSdetectAccuracy.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E8%AE%A1%E7%AE%97%E6%A3%80%E6%B5%8B%E5%87%BA%E7%9A%84qrs%E7%9A%84%E6%95%8F%E6%84%9F%E5%BA%A6qrs-sensitivity%E5%92%8C%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E5%80%BCqrs-positive-predictivity">计算检测出的QRS的敏感度（QRS sensitivity）和阳性预测值（QRS positive predictivity）</h1>
<h2 id="%E7%AC%AC%E4%B8%83%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第七次作业）</h2>
<h2 id="%E4%B8%80%E6%A3%80%E6%B5%8Bqrs%E6%B3%A2%E9%80%BB%E8%BE%91%E7%9A%84%E6%94%B9%E8%BF%9B">一、检测QRS波逻辑的改进</h2>
<h3 id="1%E5%9B%BA%E5%AE%9A%E9%98%88%E5%80%BC%E6%B3%95%E9%98%88%E5%80%BC%E8%AE%BE%E4%B8%BA2000">1.固定阈值法（阈值设为2000）</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">int</span> thx = <span class="hljs-number">2000</span>;  <span class="hljs-comment">//设一个阈值 </span>

    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//获得上升沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h3 id="2%E6%94%B9%E5%8F%98%E9%98%88%E5%80%BC%E6%B3%95%E6%9C%80%E5%A4%A7%E5%80%BC%E7%B3%BB%E6%95%B0%E4%B8%BA02">2.改变阈值法（最大值系数为0.2）</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">max</span> = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span>(a &lt; <span class="hljs-number">8</span>)  <span class="hljs-comment">//取前八个周期</span>
    { 
        <span class="hljs-keyword">if</span> (i&lt;<span class="hljs-number">500</span>)
        {
            thr[i] = y;
            i++;
        }
        <span class="hljs-keyword">else</span>  <span class="hljs-comment">//当取了两秒后</span>
        {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> s= <span class="hljs-number">0</span>; s &lt; <span class="hljs-number">500</span>; s++)
            {
                <span class="hljs-keyword">if</span> (thr[s]&gt;<span class="hljs-built_in">max</span>)
                {
                    <span class="hljs-built_in">max</span> = thr[s];  <span class="hljs-comment">//找这两秒内的最大值</span>
                }
            }
            su = su + <span class="hljs-built_in">max</span>;  <span class="hljs-comment">//累加最大值</span>
            thx = (su * <span class="hljs-number">0.2</span>)/(a+<span class="hljs-number">1</span>);  <span class="hljs-comment">//求平均，乘阈值系数0.2</span>
            i=<span class="hljs-number">0</span>;
            a++;
        }
    }
    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h2 id="3%E5%9C%A8%E8%87%AA%E9%80%82%E5%BA%94%E9%98%88%E5%80%BC%E5%9F%BA%E7%A1%80%E4%B8%8A%E8%B0%83%E5%8F%82%E5%8A%A0%E5%85%A5%E5%BB%B6%E6%97%B6%E9%98%B2%E8%AF%AF%E5%88%A4%E6%94%B9%E5%8F%98%E5%91%A8%E6%9C%9F">3.在自适应阈值基础上调参+加入延时防误判+改变周期</h2>
<h3 id="%E9%98%88%E5%80%BC%E7%B3%BB%E6%95%B0015%E9%98%88%E5%80%BC%E8%AE%BE%E4%B8%BA%E5%89%8D%E5%85%AB%E7%A7%92%E5%86%85%E5%85%AB%E4%B8%AA%E6%9C%80%E5%A4%A7%E5%80%BC%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0%E9%98%88%E5%80%BC%E7%B3%BB%E6%95%B0">阈值系数0.15；阈值设为前八秒内八个最大值的中位数*阈值系数</h3>
<h3 id="%E6%AF%8F%E6%AC%A1%E5%BB%B6%E6%97%B620%E4%B8%AA%E7%82%B9%E5%86%8D%E5%88%87%E6%8D%A2%E5%AF%BB%E6%89%BE%E4%B8%8A%E5%8D%87%E6%94%AF%E4%B8%8B%E9%99%8D%E6%94%AF%E7%8A%B6%E6%80%81%E5%91%A8%E6%9C%9F%E5%8F%98%E4%B8%BA%E4%B8%80%E7%A7%92">每次延时20个点再切换寻找上升支/下降支状态；周期变为一秒</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> y = qrsfilter(x); <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">1</span>;           <span class="hljs-comment">//初始是1状态</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">max</span> = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> (a &lt; <span class="hljs-number">8</span>) <span class="hljs-comment">//取前八个周期</span>
        {
            <span class="hljs-keyword">if</span> (i &lt; <span class="hljs-number">250</span>)
            {
                thr[i] = y;
                i++;
            }
            <span class="hljs-keyword">else</span> <span class="hljs-comment">//当取了一秒后</span>
            {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> s = <span class="hljs-number">0</span>; s &lt; <span class="hljs-number">250</span>; s++)
                {
                    <span class="hljs-keyword">if</span> (thr[s] &gt; <span class="hljs-built_in">max</span>)
                    {
                        <span class="hljs-built_in">max</span> = thr[s]; <span class="hljs-comment">//找这一秒内的最大值</span>
                    }
                }
                maxm[a] = <span class="hljs-built_in">max</span>;
                i = <span class="hljs-number">0</span>;
                a++;
                su = su + <span class="hljs-built_in">max</span>;              <span class="hljs-comment">//累加最大值</span>
                thx = (su * <span class="hljs-number">0.15</span>) / (a + <span class="hljs-number">1</span>); <span class="hljs-comment">//求平均，乘阈值系数0.15</span>
            }
        }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(a == <span class="hljs-number">8</span>)
        {
            thx = mid(maxm, a) * <span class="hljs-number">0.15</span>;  <span class="hljs-comment">//找中位数，乘阈值系数0.15</span>
            a = <span class="hljs-number">9</span>;
        }
    <span class="hljs-keyword">switch</span> (star)
    {
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:          <span class="hljs-comment">//1状态的时候 寻找上升支？</span>
            <span class="hljs-keyword">if</span> (y &gt; thx) <span class="hljs-comment">//超过阈值（上升支）</span>
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">4</span>; <span class="hljs-comment">//切换为4状态</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:          <span class="hljs-comment">//2状态的时候 寻找下降支？</span>
            <span class="hljs-keyword">if</span> (y &lt; thx) <span class="hljs-comment">//低于阈值（下降支）</span>
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">3</span>; <span class="hljs-comment">//切换为3状态</span>
                <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; <span class="hljs-comment">//获得下降沿的阈值临界点</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-comment">//这里是不是在防止误判？隔20个点后再寻找上升沿</span>
            pre++;
            <span class="hljs-keyword">if</span> (pre == <span class="hljs-number">20</span>)
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//去寻找上升支</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:  <span class="hljs-comment">//防止误判 隔20个点后再寻找下降沿</span>
            pre++;
            <span class="hljs-keyword">if</span> (pre == <span class="hljs-number">20</span>)
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">2</span>;  <span class="hljs-comment">//去寻找下降支</span>
            }
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-comment">//其他时刻返回9</span>
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">mid</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> x)</span> <span class="hljs-comment">//找中位数</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> b[x];
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> median;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; x - <span class="hljs-number">1</span>; ++i)  <span class="hljs-comment">//排序</span>
    {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; x - i - <span class="hljs-number">1</span>; ++j)
        {
            <span class="hljs-keyword">if</span> (a[j] &gt; a[j + <span class="hljs-number">1</span>])
            {
                <span class="hljs-keyword">int</span> t = a[j];
                a[j] = a[j + <span class="hljs-number">1</span>];
                a[j + <span class="hljs-number">1</span>] = t;
            }
        }
    }
    <span class="hljs-keyword">if</span> (x % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>)
        median = (a[(x / <span class="hljs-number">2</span>) - <span class="hljs-number">1</span>] + a[x / <span class="hljs-number">2</span>]) / <span class="hljs-number">2</span>;  <span class="hljs-comment">//偶数时的中位数 </span>
    <span class="hljs-keyword">else</span>
        median = a[(x - <span class="hljs-number">1</span>) / <span class="hljs-number">2</span>];  <span class="hljs-comment">//奇数时的中位数</span>
    <span class="hljs-keyword">return</span> median; 
}
</div></code></pre>
<h2 id="%E4%BA%8C%E6%A3%80%E6%B5%8B%E7%A8%8B%E5%BA%8F">二、检测程序</h2>
<h3 id="1%E4%B8%BB%E7%A8%8B%E5%BA%8F%E8%BE%93%E5%87%BAate%E6%96%87%E4%BB%B6">1.主程序——输出ate文件</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"wfdb/wfdb.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"wfdb/ecgmap.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> delay 40 <span class="hljs-comment">//滤波延时</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc , <span class="hljs-keyword">char</span> *argv[])</span>
</span>{ 
    <span class="hljs-keyword">char</span> *record = argv[<span class="hljs-number">1</span>];

    <span class="hljs-comment">//wfdb库对ate文件的设定初始化</span>
    WFDB_Anninfo an[<span class="hljs-number">2</span>]; 
    WFDB_Annotation annot;
    an[<span class="hljs-number">0</span>].name = <span class="hljs-string">"ate"</span>;
    an[<span class="hljs-number">0</span>].stat = WFDB_WRITE;

    <span class="hljs-comment">//设定路径</span>
    <span class="hljs-keyword">char</span> path[<span class="hljs-number">200</span>] = <span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/"</span>;
    setwfdb(path);
    <span class="hljs-keyword">char</span> fullname[<span class="hljs-number">200</span>];
    <span class="hljs-built_in">sprintf</span>(fullname,<span class="hljs-string">"%s%s.dat"</span>,path,record); <span class="hljs-comment">//字符串合并</span>
    FILE *fp = fopen(fullname,<span class="hljs-string">"rb"</span>); <span class="hljs-comment">//读文件</span>

    <span class="hljs-comment">//FILE *fp = fopen("G:/sss/SmartHealth-master/DATA/mitdb/100.dat","rb");</span>

    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"file open error"</span>); 
    
    <span class="hljs-keyword">if</span> (annopen(record, an, <span class="hljs-number">1</span>) &lt; <span class="hljs-number">0</span>) <span class="hljs-comment">//不知道在干什么</span>
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"annopen error"</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
    }

    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        <span class="hljs-keyword">int</span> evt = PushData(x);  <span class="hljs-comment">//获得心率和QRS波位置</span>
      
        <span class="hljs-keyword">if</span>(evt&amp;EVENT_1sec)  <span class="hljs-comment">//每过一秒</span>
        {
            <span class="hljs-keyword">int</span> hr =GetCurHr();  <span class="hljs-comment">//返回心率值</span>
            <span class="hljs-comment">//fwrite(&amp;hr,1,sizeof(int),fp_hr);</span>
        }
        <span class="hljs-keyword">if</span> (evt&amp;EVENT_QRS)  <span class="hljs-comment">//每检测到QRS波的时候</span>
        {
            <span class="hljs-keyword">int</span> po = GetQRSPos();  <span class="hljs-comment">//返回QRS波位置</span>
            <span class="hljs-comment">//fwrite(&amp;po,1,sizeof(int),fp_po);</span>
            <span class="hljs-comment">//又是ate文件的设置</span>
            annot.anntyp = NORMAL;
            annot.time = po - <span class="hljs-built_in">delay</span> - <span class="hljs-number">20</span>;  <span class="hljs-comment">//20是第三种方法的延时</span>
            annot.aux = <span class="hljs-literal">NULL</span>;
            putann(<span class="hljs-number">0</span>,&amp;annot);
        }
    }
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    oannclose(<span class="hljs-number">0</span>);  <span class="hljs-comment">//还是库文件的关闭？</span>
    <span class="hljs-comment">// fclose(fp_po);</span>
    <span class="hljs-comment">// fclose(fp_hr);</span>

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;
}
</div></code></pre>
<h3 id="2%E9%80%9A%E8%BF%87matlab%E8%BE%93%E5%87%BA%E5%B9%B6%E8%B0%83%E7%94%A8bxb%E5%87%BD%E6%95%B0">2.通过Matlab输出并调用bxb函数</h3>
<pre class="hljs"><code><div>flist = dir(<span class="hljs-string">'*.dat'</span>);
<span class="hljs-keyword">for</span> i1=<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(flist)
    a = flist(i1).name;
    record = a(<span class="hljs-number">1</span>:<span class="hljs-keyword">end</span><span class="hljs-number">-4</span>);
    cmd = sprintf(<span class="hljs-string">'text %s'</span>,record);
    system(cmd);
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">for</span> i1=<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(flist)
    a = flist(i1).name;
    record = a(<span class="hljs-number">1</span>:<span class="hljs-keyword">end</span><span class="hljs-number">-4</span>);
    cmd = sprintf(<span class="hljs-string">'bxb -r %s -a atr ate -L 1.txt 2.txt'</span>,record);
    system(cmd);
<span class="hljs-keyword">end</span>
system(<span class="hljs-string">'sumstats 1.txt'</span>)
system(<span class="hljs-string">'sumstats 1.txt&gt;result.txt'</span>)
</div></code></pre>
<h2 id="%E4%B8%89%E6%A3%80%E6%B5%8B%E7%BB%93%E6%9E%9C">三、检测结果</h2>
<h3 id="1%E5%9B%BA%E5%AE%9A%E9%98%88%E5%80%BC%E6%B3%95">1.固定阈值法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69109%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879786">（平均敏感度：91.09%；平均阳性预测率：97.86%）</h3>
<p><img src="result(2000).jpg" alt=""></p>
<h3 id="2%E6%94%B9%E5%8F%98%E9%98%88%E5%80%BC%E6%B3%95">2.改变阈值法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69859%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879919">（平均敏感度：98.59%；平均阳性预测率：99.19%）</h3>
<p><img src="result(0.2).jpg" alt=""></p>
<h3 id="3%E6%94%B9%E8%BF%9B%E6%B3%95">3.改进法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69952%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879926">（平均敏感度：99.52%；平均阳性预测率：99.26%）</h3>
<p><img src="result(0.15).jpg" alt=""></p>

</body>
</html>
